package com.server.userCenter.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.validator.internal.util.privilegedactions.SetAccessibility;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.config.AppReturn;
import com.config.ConstDefine;
import com.server.pay.PayService;
import com.server.user.UserDAO;
import com.server.userCenter.RewardDAO;
import com.server.userCenter.RewardUtil;
import com.server.userCenter.UserCenterDAO;
import com.server.userCenter.UserCenterService;
import com.server.wx.WxUtil;
import com.util.IOUtil;
import com.util.StrKit;

@Component
public class UserCenterServiceIMPL implements UserCenterService{

	@Autowired
	UserCenterDAO ucdao;
	@Autowired
	UserDAO userDao;
	@Autowired
	PayService payService;
	@Autowired
	RewardUtil rewardUtil;
	@Autowired
	RewardDAO rewardDao;
	@Autowired
	WxUtil wxUtil;
	
	/**
	 * 控制用户默认收获地址
	 * @param user
	 * @param defaultId
	 */
	private void editDefaultAddr(Map<String, Object> user,String defaultId)
	{
		String userid = user.get("id").toString();
		//表试设置该地址为默认地址
		if(StrKit.notBlank(defaultId))
		{
			ucdao.setDefaultAddr(defaultId, userid);
		}else{
			//判断如果没有默认收货地址，则将最后一个设置成默认地址
			Map<String, Object> defaultAddr = ucdao.getDefaultAddr(userid);
			if(defaultAddr==null)
			{
				List<Map<String, Object>> addrList = getAddr(user);
				if(addrList!=null&&addrList.size()>0)
				{
					Map<String, Object> add1 = addrList.get(0);
					ucdao.setDefaultAddr((String)add1.get("id"), userid);
				}
			}
			
			
		}
	}
	//查询用户收获地址列表
	@Override
	public List<Map<String, Object>> getAddr(Map<String, Object> user) 
	{
		List<Map<String, Object>> list = ucdao.getUserAddr(user.get("id").toString());
		
		return list;
	}

	//新增收获地址
	@Override
	public AppReturn addAddr(Map<String, Object> user, String province, String city, String area, String addr,String phone, String name,String isdefault) 
	{
		String id=IOUtil.getId();
		int res = ucdao.addAddr(id, user.get("id").toString(), province, city, area, addr, phone, name,isdefault);
		//处理下默认收获地址
		editDefaultAddr(user, id);
		return StrKit.int2ret(res);
	}

	//编辑地址
	@Override
	public AppReturn editAddr(Map<String, Object> user, String id, String province, String city, String area,String addr, String phone, String name, String status,String isdefault) 
	{
		Map<String, Object> reqMap = new HashMap<String, Object>();
		reqMap.put("userid", user.get("id").toString());
		reqMap.put("id", id);
		if(StrKit.notBlank(province))reqMap.put("province", province);
		if(StrKit.notBlank(city))reqMap.put("city", city);
		if(StrKit.notBlank(area))reqMap.put("area", area);
		if(StrKit.notBlank(addr))reqMap.put("addr", addr);
		if(StrKit.notBlank(phone)&&!phone.contains("*"))reqMap.put("phone", phone);//避免修改地址是
		if(StrKit.notBlank(name))reqMap.put("name", name);
		if(StrKit.notBlank(status))reqMap.put("status", status);
		if(StrKit.notBlank(isdefault))reqMap.put("isdefault", isdefault);
		int res = ucdao.upAddr(reqMap);
		
		//处理下默认收获地址
		String defaultId = null;
		if(StrKit.notBlank(isdefault)&&isdefault.equals("1"))defaultId=id;
		editDefaultAddr(user, defaultId);
		
		return StrKit.int2ret(res);
	}

	//设置默认地址
	@Override
	public AppReturn setDefaultAddr(Map<String, Object> user, String id) 
	{
		editDefaultAddr(user, id);
		
		return new AppReturn("00");
	}
	
	//获取默认地址
	@Override
	public AppReturn getDefaultAddr(Map<String, Object> user) 
	{
		Map<String, Object> addr = ucdao.getDefaultAddr(user.get("id").toString());
		
		return StrKit.map2AppReturn(addr);
	}
	
	//查询订单
	@Override
	public AppReturn getList(Map<String, Object> user, String status) 
	{
		//查询订单列表
		Map<String, Object> reqMap = new HashMap<String, Object>();
		reqMap.put("userid", user.get("id").toString());
		if(StrKit.notBlank(status))reqMap.put("status", status);
		List<Map<String, Object>> list = ucdao.getList(reqMap);
		JSONArray jsa = StrKit.list2jsonArr(list);
		//循环订单列表，查询商品列表
		
		if(jsa!=null&&jsa.length()>0)
		{
			for(int i=0;i<jsa.length();i++)
			{
				JSONObject listData = jsa.getJSONObject(i);
				String listid=listData.getString("id");
				List<Map<String, Object>> listGoods = ucdao.getListGoods(listid);
				JSONArray goodsJsa=StrKit.list2jsonArr(listGoods);
				listData.put("info", goodsJsa);
			}
		}
		
		return new AppReturn("00", "", jsa);
	}
	
	//查询订单详情
	@Override
	public AppReturn getListDetail(Map<String, Object> user,String id)
	{
		Map<String, Object> list = ucdao.getListDetail(id);
		if(list==null||!list.containsKey("id"))return new AppReturn("05","订单号有误！");
		List<Map<String, Object>> listGoods = ucdao.getListGoods(id);
		JSONObject data = StrKit.map2json(list);
		data.put("info", StrKit.list2jsonArr(listGoods));
		
		
		return new AppReturn("00","",data);
	}
	//查询交易记录
	@Override
	public List<Map<String, Object>> getAmountLog(Map<String, Object> user) 
	{
		List<Map<String, Object>> list = ucdao.getAmountLog(user.get("id").toString(), "1", null, null, null);
		
		return list;
	}
	//取消订单
	@Override
	public AppReturn cancelList(Map<String, Object> user, String id)
	{
		//获取订单
		Map<String, Object> map = ucdao.getListDetail(id);
		//判断状态
		if(map==null||!map.containsKey("id"))return new AppReturn("06", "订单信息有误！");
		if(!map.get("status").equals("1"))return new AppReturn("07", "订单已经走入流程，无法取消！");
		
		//更新订单状态
		map.put("status", "9");
		int res = ucdao.upList(map);
		
		return StrKit.int2ret(res);
	}
	//删除订单
	@Override
	public AppReturn delList(Map<String, Object> user, String id) 
	{
		//获取订单
		Map<String, Object> map = ucdao.getListDetail(id);
		//判断状态
		if(map==null||!map.containsKey("id"))return new AppReturn("06", "订单信息有误！");
//		if(!map.get("status").equals("1"))return new AppReturn("07", "订单已经走入流程，无法取消！");
		
		//更新订单状态
		map.put("status", "99");
		int res = ucdao.upList(map);
		
		return StrKit.int2ret(res);		
	}
	//查询我的会员
	@Override
	public List<Map<String, Object>> myMember(Map<String, Object> user) 
	{
		List<Map<String, Object>> list = ucdao.getDirectUser(user.get("tjcode").toString());
		//查询每个直属会员得销售量
		if(list!=null&&list.size()>0)
		{
			for(int i=0;i<list.size();i++)
			{
				Map<String, Object> map = list.get(i);
				String userid = map.get("id").toString();
				String tjtree = map.get("tjtree").toString()+"#"+map.get("tjcode").toString();
				Map<String, Object> sellmap = ucdao.countTeamSell(tjtree, userid);
				map.put("amount_all",ConstDefine.DF0.format(sellmap.get("su")));
			}
		}
		
		return list;
	}
	//实名制
	@Override
	public AppReturn realname(Map<String, Object> user, String realname, String cardcode) 
	{
		
		
		return null;
	}
	//转账
	@Override
	public AppReturn transferAmount(Map<String, Object> user, String reciveId, String amount, String pwd) 
	{
		//判断密码
		if(!user.get("pay_pwd").equals(pwd))return new AppReturn("05","密码错误！");
//		if(!user.get("pwd").equals(pwd))return new AppReturn("05","密码错误！");
		
		//判断金额
		double amount_tx = (Double)user.get("amount_tx");
		double amountD=Double.parseDouble(amount);
		if(amountD>amount_tx)
		{
			return new AppReturn("06","金额不足错误！");
		}
		//收款人
		Map<String, Object> skuser=userDao.getUserById(reciveId);
		if(skuser==null)return new AppReturn("07","收款人信息错误！");
		
		//执行转账
		ucdao.doTransfer(user.get("id").toString(), reciveId, amount);
		//插入交易记录
		//转账人
		ucdao.insAmountLog(IOUtil.getId(), user.get("id").toString(), "2",Double.parseDouble(amount) , "13", "转账给"+reciveId,reciveId);
		//收款人
		ucdao.insAmountLog(IOUtil.getId(), skuser.get("id").toString(), "1",Double.parseDouble(amount) , "18", "他人转入",user.get("id").toString());
		
		return new AppReturn("00");
	}
	
	//余额支付
	@Override
	@Transactional
	public AppReturn payOffLine(Map<String, Object> user, String listid,String pwd,String type) 
	{
		
		//验证密码
		if(!user.get("pay_pwd").equals(pwd.trim()))return new AppReturn("06", "密码错误！");
//		if(!user.get("pwd").equals(pwd.trim()))return new AppReturn("06", "密码错误！");
		
		Map<String, Object> list = ucdao.getListDetail(listid);//订单详情
		String userid= user.get("id").toString();
		//判断订单状态
		if(!list.get("status").equals("1"))return new AppReturn("01", "订单状态错误！");
		double amount = (Double)list.get("amount");
		
		
		//判断金额
		double amount_tx = (Double)user.get("amount_tx");
		double amount_gw = (Double)user.get("amount_gw");
		int point = Integer.parseInt(user.get("point").toString());
		//判断支付类型
		if(type.equals("1"))//余额支付
		{
			if(amount_tx<amount)return new AppReturn("03", "余额不足！");
			//扣款
			int payRes = ucdao.upUserAmount(userid, -amount, 0, 0);
			if(payRes<1)return new AppReturn("05", "支付失败！");
			//插入金额记录
			int logRes = ucdao.insAmountLog(IOUtil.getId(), userid, "2", amount, "11", "购买商品",listid);
			if(logRes>0){
				//执行付款订单
				System.out.println("执行付款后操作！");
				payService.doListPayBack(listid, "", "","3");
			}
			return StrKit.int2ret(logRes);
		}else if(type.equals("2")){//购物金支付
			
//			return new AppReturn("06", "暂不支持购物金支付！");
			//判断金额够不够
			if(amount_gw<amount)return new AppReturn("03", "购物金不足！");
			//判断商品是否都是都是购物金商品
			boolean ifgwjGoods = true;//标记是否都是购物金物品
			List<Map<String, Object>> listGoods = ucdao.getListGoods(listid);
			if(listGoods!=null&&listGoods.size()>0)	
			{
				for(int i=0;i<listGoods.size();i++)
				{
					Map<String, Object> goods = listGoods.get(i);
					if(goods.get("ifreward").equals("1")){
						ifgwjGoods=false;
						break;
					}
				}
				
			}
			if(!ifgwjGoods)return new AppReturn("09","购物金只可以购买指定商品！");
			
			//扣款
			int payRes = ucdao.upUserAmount(userid, 0, -amount, 0);
			if(payRes<1)return new AppReturn("05", "支付失败！");
			//插入金额记录
			int logRes = ucdao.insAmountLog(IOUtil.getId(), userid, "2", amount, "11", "购买商品",listid);
			if(logRes>0){
				//执行付款订单
				System.out.println("执行付款后操作！");
				payService.doListPayBack(listid, "", "","4");
			}
			return StrKit.int2ret(logRes);
		}else if(type.equals("3")){//购物金+积分支付
			double amountReal = amount*0.7;//实际需要支付的购物金
			int pointReal = (int) (amount*0.3);//实际需要支付的积分
			//判断金额够不够一半
			if(amount_gw<(amountReal))return new AppReturn("03", "购物金不足！");
			//积分不够另外一半
			if(point<(pointReal))
			{
				//判断购物金+剩余积分，是否足够支付
				if((point+amount_gw)<amount)return new AppReturn("06", "积分不足！");
				pointReal = point;
				amountReal = amount - point;
			}
			//判断商品是否都是都是购物金商品
			boolean ifgwjGoods = true;//标记是否都是购物金物品
			List<Map<String, Object>> listGoods = ucdao.getListGoods(listid);
			if(listGoods!=null&&listGoods.size()>0)	
			{
				for(int i=0;i<listGoods.size();i++)
				{
					Map<String, Object> goods = listGoods.get(i);
					if(goods.get("ifreward").equals("1")){
						ifgwjGoods=false;
						break;
					}
				}
				
			}
			if(!ifgwjGoods)return new AppReturn("09","购物金只可以购买指定商品！");
			
			//扣款
			int payRes = ucdao.upUserAmount(userid, 0, -amountReal, 0);
			if(payRes<1)return new AppReturn("05", "支付失败！");
			//扣积分
			ucdao.upUserPoint(userid, -pointReal);
			//插入金额记录
			int logRes = ucdao.insAmountLog(IOUtil.getId(), userid, "2", amountReal, "11", "购买商品",listid);
			ucdao.insPointLog(IOUtil.getId(), userid, "2", pointReal, "11", "购买商品", listid);
			if(logRes>0){
				//执行付款订单
				System.out.println("执行付款后操作！");
				payService.doListPayBack(listid, "", "","5");
			}
			return StrKit.int2ret(logRes);
			
		}
		
		return new AppReturn("10","数据有误！");
	}
	//根据推荐码或手机号查询用户
	@Override
	public AppReturn getUserByCP(String serchStr) 
	{
		List<Map<String, Object>> list = ucdao.getUserByCP(serchStr);
		
		return StrKit.list2Appreturn(list);
	}
	//更新用户名称或头像
	@Override
	public AppReturn editUser(Map<String, Object> user, String realName, String head,String phone) 
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", user.get("id").toString());
		if(StrKit.notBlank(realName))map.put("realname", realName);
		if(StrKit.notBlank(head))map.put("head", head);
		if(StrKit.notBlank(phone))map.put("phone", phone);
		int res = ucdao.updateUser(map);
		
		if(res>0)//修改成功，将用户信息传过去
		{
			user = userDao.getUserById(user.get("id").toString());
			JSONObject data = StrKit.map2json(user);
			return new AppReturn("00", "操作成功", data);
		}else{
			return new AppReturn("01", "操作失败");
		}
		
	}
	//更新密码
	@Override
	public AppReturn changePWD(Map<String, Object> user, String oldpwd, String newpwd) 
	{
		int res = ucdao.updatePwd(user.get("id").toString(), oldpwd, newpwd);
		
		if(res>0)//修改成功，将用户信息传过去
		{
			user = userDao.getUserById(user.get("id").toString());
			JSONObject data = StrKit.map2json(user);
			return new AppReturn("00", "操作成功", data);
		}else{
			return new AppReturn("01", "操作失败");
		}
	}
	//更新密码
	@Override
	public AppReturn changePWD_pay(Map<String, Object> user, String oldpwd, String newpwd) 
	{
		int res = ucdao.updatePwd_pay(user.get("id").toString(), oldpwd, newpwd);
		
		if(res>0)//修改成功，将用户信息传过去
		{
			user = userDao.getUserById(user.get("id").toString());
			JSONObject data = StrKit.map2json(user);
			return new AppReturn("00", "操作成功", data);
		}else{
			return new AppReturn("01", "操作失败");
		}
	}	
	//用户提现
	@Override
	public AppReturn cashOut(Map<String, Object> user, String amount,  String username, String cardCode,String bank) 
	{
		String userid=user.get("id").toString();
		double amount_sq=Double.parseDouble(amount);
		//判断金额
		double amount_tx = (Double)user.get("amount_tx");
		if(amount_sq>amount_tx)return new AppReturn("01","余额不足！");
		
		//判断金额需要大于100
		if(amount_tx<100)return new AppReturn("03","余额需要大于100才可以提现！");
		
		//扣减金额
		int payRes = ucdao.upUserAmount(userid, -amount_sq, 0, 0);
		if(payRes<1)return new AppReturn("05", "提现失败！");
		//插入金额记录
		String id=IOUtil.getId();
		int logRes = ucdao.insAmountLog(IOUtil.getId(), userid, "2", amount_sq, "12", "申请提现",id);
		//插入记录
		ucdao.insCashLog(id, userid, bank, cardCode, username, amount,"","");
		
		return StrKit.int2ret(payRes);
	}
	//查询提现记录
	@Override
	public List<Map<String, Object>> getCashLog(Map<String, Object> user) 
	{
		List<Map<String, Object>> cashLog = ucdao.getCashLog(user.get("id").toString());
		
		return cashLog;
	}
	
	//查询我的数据
	@Override
	public Map<String, Object> getMyData(Map<String, Object> user) 
	{
		Map<String, Object> res = new HashMap<String, Object>();
		String tjtree = rewardUtil.getSelfTjtree(user);
		String userid = user.get("id").toString();
		Map<String, Object> teamSell = ucdao.countTeamSell(tjtree, userid);//团队总销售数据
		Map<String, Object> usreReward = rewardDao.getUserReward(userid);//总得到得奖金
		Map<String, Object> usreReward_jx = rewardDao.getUserReward_jx(userid,IOUtil.getdate("yyyy-MM-dd"+" 00:00:00"));//总得到得奖金
		
		res.put("teamSell", teamSell.get("su"));
		res.put("usreReward", usreReward.get("su"));
		res.put("usreReward_jx", usreReward_jx.get("su"));
		
		return res;
	}
	

	@Override
	public Map<String, Object> getDefaultAddrMap(Map<String, Object> user) 
	{
		Map<String, Object> addr = ucdao.getDefaultAddr(user.get("id").toString());
		return addr;
	}
	//查询用户礼券
	@Override
	public int countUserLQ(Map<String, Object> user) 
	{
		String userid = user.get("id").toString();
		Map<String, Object> data = ucdao.countLQ(userid);
		BigDecimal res = (BigDecimal) data.get("s");
		return res.intValue();
	}
	//根据条件查询礼券记录
	@Override
	public List<Map<String, Object>> getLQLog(Map<String, Object> user, Map<String, Object> reqData) 
	{
		String userid = user.get("id").toString();
		reqData.put("userid", userid);
		List<Map<String, Object>> data = ucdao.getLQLog(reqData);
		
		return data;
	}
	//查询艾豆明显
	@Override
	public List<Map<String, Object>> getADList(Map<String, Object> user, Map<String, Object> reqData) 
	{
		String userid = user.get("id").toString();
		reqData.put("userid", userid);
		List<Map<String, Object>> data = ucdao.getADLog(reqData);
		
		return data;
	}
	//查询艾豆分红
	@Override
	public List<Map<String, Object>> getADAmount(Map<String, Object> user, Map<String, Object> reqData) 
	{
		String userid = user.get("id").toString();
		List<Map<String, Object>> data = ucdao.getAmountLog(userid, "1", "22", reqData.get("stime")!=null?(String)reqData.get("stime"):null, reqData.get("etime")!=null?(String)reqData.get("etime"):null);
		
		return data;
	}
	//查询累计提现金额
	@Override
	public double countCashLog(Map<String, Object> user) 
	{
		String userid = user.get("id").toString();
		Map<String, Object> data = ucdao.countCashLog(userid);
		
		double res = (double)data.get("s");
		return res;
	}
	//查询个人中心首页数据
	@Override
	public Map<String, Object> getMyUcData(Map<String, Object> user) 
	{
		Map<String, Object> data = new HashMap<String, Object>();
		String userid = (String) user.get("id");
		//查询用户本月PV，历史PV
		Map<String, Object> map_now = rewardDao.getUserPV(userid, IOUtil.getdate("yyyy-MM-")+"01", null);
		Map<String, Object> map_All = rewardDao.getUserPV(userid, null, null);
		//查询团队的PV
		Map<String, Object> team_map_now = rewardDao.getTeamPV(userid, IOUtil.getdate("yyyy-MM-")+"01", null);
		Map<String, Object> team_map_all = rewardDao.getTeamPV(userid, null, null);
		
		//查询上个月艾豆
		Map<String, Object> map_ad = rewardDao.countRewardMonth(IOUtil.getLastMonth(), userid, "('"+ConstDefine.AD+"')");
		
		data.put("pv_now", map_now.get("spv"));
		data.put("pv_all", map_All.get("spv"));

		data.put("pv_now_team", team_map_now.get("spv"));
		data.put("pv_all_team", team_map_all.get("spv"));
		
		data.put("ad", map_ad.get("samount"));
		return data;
	}
	
	//查询加盟记录
	@Override
	public AppReturn getShopJoinLog(Map<String, Object> user) 
	{
		String userid = (String) user.get("id");
		List<Map<String, Object>> data = ucdao.getShopJoinList(userid);
		
		AppReturn ret = StrKit.list2Appreturn(data);
		return ret;
	}
	//取消加盟记录
	@Override
	public AppReturn cancelShopJoin(Map<String, Object> user, int id) 
	{
		int res = ucdao.delJoinLog(id);
		return StrKit.int2ret(res);
	}
	//提现
	@Override
	@Transactional
	public synchronized AppReturn doTx(Map<String, Object> user, String type) 
	{
		String userid = (String) user.get("id");
//		Map<String, Object> openidMap = userDao.getUserOpenid(userid);
//		if(openidMap==null)return new AppReturn("01","用户信息错误！！");
//		String openid = (String) openidMap.get("openid");
		String openid =(String) user.get("openid");
		System.out.println("openid:"+openid);
		String day = IOUtil.getdate("yyyy-MM-dd");
		//小额-200
		if(type.equals("1"))
		{
			double txamount = 200;
			double amount = 200+200*0.03;
			//判断用户金额够不够
			double userAmount = (double) user.get("amount_money");
			if(userAmount<amount)return new AppReturn("01","余额不足！");
			//判断当天提现几次
			Map<String, Object> txTimes = ucdao.countCashDayLog(userid, day, type);
			if(txTimes!=null)
			{
				long count = (long) txTimes.get("c");
				if(count>=ConstDefine.getTx200Time())return new AppReturn("04","已经超过提现次数，请明天再来！");
			}
			
			//扣减金额
			int res = ucdao.deuUserAmount(userid, amount);
			if(res<1)return new AppReturn("02","系统忙，请稍后再试！");
			//处理提现
			JSONObject txRet = wxUtil.doTx200(openid,ConstDefine.DF0.format(txamount*100));
			if(txRet.getString("result").equals("0"))
			{
				//如果提现失败，回滚
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return new AppReturn("03","提现失败，请稍后再试！");
			}else{
				//插入记录
				ucdao.insCashLog(userid, type, day, txamount+"","2","",txRet.getString("orderId"),txRet.getString("wxId"));
				return new AppReturn("00");
			}
		}else{
			double txamount = 500;
			double amount = 500+500*0.06;
			//判断用户金额够不够
			double userAmount = (double) user.get("amount_money");
			if(userAmount<amount)return new AppReturn("01","余额不足！");
			//判断当天提现几次
			Map<String, Object> txTimes = ucdao.countCashDayLog(userid, day, type);
			if(txTimes!=null)
			{
				long count = (long) txTimes.get("c");
				if(count>=ConstDefine.getTx500Time())return new AppReturn("04","已经超过提现次数，请明天再来！");
			}
			//扣减金额
			int res = ucdao.deuUserAmount(userid, amount);
			if(res<1)return new AppReturn("02","系统忙，请稍后再试！");
			//处理提现
			JSONObject txRet = wxUtil.doTxQY(openid, ConstDefine.DF0.format(txamount*100));
			if(txRet.getString("result").equals("0"))
			{
				//如果提现失败，回滚
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return new AppReturn("03","提现失败，请稍后再试！");
			}else{
				//插入记录
				ucdao.insCashLog(userid, type, day, txamount+"","2","",txRet.getString("orderId"),txRet.getString("wxId"));
				return new AppReturn("00");
			}
		}
	}
	//退出登录
	@Override
	public AppReturn goLogOff(Map<String, Object> user) 
	{
		//清空Openid2user关系
		String openid = (String) user.get("openid");
		System.out.println("openid:"+openid);
		int res = userDao.delOpenid(openid);
		return StrKit.int2ret(res);
	}	
}
